from tortoise.expressions import Q

from app.controllers.orders.list import orders_list_controller
from app.core.crud import CRUDBase
from app.models import BucklingTodo
from app.models.orders import OrdersList
from app.schemas.orders import OrdersListCreate, OrdersListUpdate, OrdersListCreateMini, OrdersListPay
from app.schemas.saas import PayCreate


class PayController(CRUDBase[OrdersList, OrdersListCreate, OrdersListUpdate]):
    def __init__(self):
        super().__init__(model=OrdersList)

    @staticmethod
    async def do_create(data_in: PayCreate,add_uid:int=0):
        order_in = OrdersListCreateMini(
            type = data_in.type,
            sku = data_in.sku,
            pay_designer = data_in.pay_designer,
            address_id = data_in.address_id,
            fkfs = data_in.fkfs,
        )
        ##下单
        ret = await orders_list_controller.do_create(obj_in=order_in, user_id=data_in.user_id)
        ##结算
        order_id = ret["order_id"]
        remark = data_in.remark
        await OrdersList.filter(id=order_id).update(remark=remark,add_uid=add_uid)
        is_deposit = data_in.is_deposit
        coupons = data_in.coupons
        if coupons:
            coupons = [{'id': x} for x in coupons]
        data_in_pay = OrdersListPay(
            id = order_id,
            is_deposit = is_deposit,
            coupons = coupons
        )
        await orders_list_controller.order_pay(data_in=data_in_pay,pay_type=1)
        return ret



pay_controller = PayController()
